import { createRouter, createWebHashHistory } from 'vue-router';
import easyapi from '../api';

const isLogin = async function(to,from){
  //获取token
  let token=sessionStorage.getItem("easytoken");
  //验证token是否有效
  let result=await easyapi.get("/api/checktoken");
  if(!token || result.code!=200){
      return {path:'/login'};
  }
}


const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    { path: "/", redirect: "/login" },
    { path: '/one',component: () => import('../settype/one.vue'),beforeEnter:isLogin},
    { path: '/register',component: () => import('../settype/register.vue')},
    { path: '/login', component: () => import("../settype/login.vue")},
    {
      path: '/home',
      component: () => import('../settype/home.vue'),
      children: [
        {
          path: '/employees',
          component: () => import('../views/employees.vue')
        },
        {
          path: '/rooms',
          component: () => import('../views/rooms.vue')
        },
        {
          path: '/reservations',
          component: () => import('../views/reservations.vue')
        },
        {
          path: '/checkins',
          component: () => import('../views/checkins.vue')
        },
        {
          path: '/guests',
          component: () => import('../views/guests.vue')
        },
        {
          path: '/user',
          component: () => import('../views/user.vue')
        },
        {
          path: '/tubiao',
          component: () => import('../views/tubiao.vue')
        }
      ],beforeEnter:isLogin
    }
  ]
});

export default router;